package com.wandian.news.mvvm.wxtop;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mylib.libcore.adapter.BasePagerAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.databinding.NewsFragmentToutiaoBinding;
import com.wandian.news.databinding.NewsFragmentWxTopBinding;
import com.wandian.news.mvvm.wxtop.wxfeaturedpage.WXFeaturedPageFragment;
import com.wandian.news.mvvm.wxtop.wxtoppage.WXTopPageFragment;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.autosize.utils.ScreenUtils;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 微信热文fragment
 **/
@Route(path = RouterConfig.ROUTER_FRAGMENT_NEWS_WX_TOP_PAGE)
public class WXTopFragment extends BaseMVVMFragment<WXTopFragmentViewModel> {
    private NewsFragmentWxTopBinding dataBinding;
    private BasePagerAdapter myAdapter;
    private List<String> titleList=new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.inflate(inflater, R.layout.news_fragment_wx_top, container, false);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);
        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) dataBinding.viewSys.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.height = ScreenUtils.getStatusBarHeight();;
        dataBinding.viewSys.setLayoutParams(linearParams);
        initViewData();
    }


    private void initViewData(){
        titleList.clear();
        fragments.clear();
        titleList.add("热文");
        titleList.add("精选");
        titleList.add("搞笑");
        titleList.add("话题");
        titleList.add("古通");
        fragments.add(WXFeaturedPageFragment.newInstance("热文"));
        fragments.add(WXFeaturedPageFragment.newInstance("精选"));
        fragments.add(WXFeaturedPageFragment.newInstance("段子"));
        fragments.add(WXFeaturedPageFragment.newInstance("话题"));
        fragments.add(WXFeaturedPageFragment.newInstance("古通"));
        initTabPage();
    }

    //初始化tabpage
    private void initTabPage() {
        myAdapter = new BasePagerAdapter(getChildFragmentManager(), fragments,titleList);
        if(UIUtils.isEmpty(dataBinding.viewPager.getAdapter())){
            dataBinding.viewPager.setAdapter(myAdapter);
            dataBinding.viewPager.setOffscreenPageLimit(titleList.size());
            dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        }
        if(!UIUtils.isEmpty(myAdapter)){
            myAdapter.notifyDataSetChanged();
        }
        //预加载数量
        dataBinding.viewPager.setOffscreenPageLimit(1);
        dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        dataBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


}
